Visualization Packages¶

Rendering HTML¶

In [ ]:
import plotly
plotly.offline.init_notebook_mode()

Using Matplotlib¶

1. plot (x,y) - Plots a line from point(x axis) to point (y axis)¶

Sample Data Table¶

X Y
0 4
10 4.68

Learn more on line plots

In [ ]:
import matplotlib.pyplot as plt
import numpy as np
# make data
x = np.linspace(0, 10, 100)
y = 4 + 2 * np.sin(2 * x)

# plot
fig, ax = plt.subplots()

ax.plot(x, y, linewidth=2.0)

ax.set(xlim=(0, 8), xticks=np.arange(1, 8),
       ylim=(0, 8), yticks=np.arange(1, 8))

plt.show()

2. scatter(x,y) - Draws a scatter plot¶

X Y
4.8 2.5

Learn more on scatter plot

In [ ]:
plt.style.use('_mpl-gallery')

# make the data
np.random.seed(3)
x = 4 + np.random.normal(0, 2, 24)
y = 4 + np.random.normal(0, 2, len(x))
# size and color:
sizes = np.random.uniform(15, 80, len(x))
colors = np.random.uniform(15, 80, len(x))

# plot
fig, ax = plt.subplots()

ax.scatter(x, y, s=sizes, c=colors, vmin=0, vmax=100)

ax.set(xlim=(0, 8), xticks=np.arange(1, 8),
       ylim=(0, 8), yticks=np.arange(1, 8))

plt.show()

Using Seaborn¶

1. Seaborn.lmplot() method is used to plot data and draw regression model fits across grids where multiple plots can be plotted.¶

Leqrn more on Seaborn Implot

In [ ]:
import seaborn as sns
sns.set_theme(style="ticks")

# Load the example dataset 
df = sns.load_dataset("anscombe")

sns.lmplot(
    data=df, x="x", y="y", col="dataset", hue="dataset",
    col_wrap=2, palette="muted", ci=None,
    height=4, scatter_kws={"s": 50, "alpha": 1}
)
Out[ ]:
<seaborn.axisgrid.FacetGrid at 0x21a6dffac10>

2. Seaborn.displot() - visualizing univariate and bivariate distribution of data¶

Learn more on Seaborn Displot

In [ ]:
sns.set_theme(style="darkgrid")
df = sns.load_dataset("penguins")
sns.displot(
    df, x="flipper_length_mm", col="species", row="sex",
    binwidth=3, height=3, facet_kws=dict(margin_titles=True),
)
Out[ ]:
<seaborn.axisgrid.FacetGrid at 0x21a6eb57650>

Using Plotly¶

1. Bar chart with Plotly Express¶

Learn more on Plotly bar chart

In [ ]:
import plotly.express as px
df = px.data.tips()
fig = px.bar(df, x="sex", y="total_bill", color="smoker", barmode="group")
fig.show()

2. Scatter Matrix with Plotly Express¶

Learn more on Scatter Matrix

In [ ]:
df = px.data.iris()
fig = px.scatter_matrix(df, dimensions=["sepal_width", "sepal_length", "petal_width", "petal_length"], color="species")
fig.show()

Adding Image¶

In [ ]:
from PIL import Image
img = Image.open('D:\AI & ML\ML FOUNDATION\LABS\CSCN-8010-labs\images\image1.png')
fig = plt.imshow(img)
plt.axis('off')
fig.axes.get_xaxis().set_visible(False)
fig.axes.get_yaxis().set_visible(False)